package com.songqintuan.platform.page;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.songqintuan.common.bean.Page;
import com.songqintuan.common.page.IPojoPage;
import com.songqintuan.common.page.PojoPage;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.CustomerLevel;
import com.songqintuan.persist.service.hibernate.ICustomerLevelService;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

public class CustomerLevelPage extends PojoPage<CustomerLevel, String> implements
		IPojoPage<CustomerLevel, String>, java.io.Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void getPageDatas() {
		Page<CustomerLevel, String> curPage = new Page<CustomerLevel, String>();
		Integer pPageNo = this.getPageNo();
		if (null == pPageNo) {
			pPageNo = new Integer(1);
		}
		Integer pPageSize = this.getPageSize();
		if (null == pPageSize) {
			pPageSize = new Integer(PlatformConstants.DEFAULT_ONE_PAGE_NUMBER);
		}
		curPage = this.getData(pPageNo, pPageSize, this.getParam());
		SelectItem[] tempPageItem = new SelectItem[(int) curPage
				.getTotalPageCount()];
		for (int j = 0; j < tempPageItem.length; j++) {
			tempPageItem[j] = new SelectItem(new Integer(j + 1),
					String.valueOf(j + 1));
		}
		this.setPageItems(tempPageItem);
		this.setPage(curPage);
	}

	public CustomerLevelPage(Integer pageNo, Integer pageSize, CustomerLevel param) {
		super();
		this.setPageSize(pageSize);
		this.setPageNo(pageNo);
		this.setParam(param);

	}

	// 所有的查询均根据param的值确定
	public Page<CustomerLevel, String> getData(int pageNo, int pageSize, CustomerLevel param) {// param
																					// is
																					// statuscode
																					// and
																					// customer
		Page<CustomerLevel, String> resultPage = new Page<CustomerLevel, String>();
		ICustomerLevelService<CustomerLevel, String> objService = (ICustomerLevelService<CustomerLevel, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.CUSTOMER_LEVEL_SERVICE_BEAN_NAME);
		objService.setEntityClass(CustomerLevel.class);
		objService.setPKClass(String.class);
		Criteria criteria = objService.createCriteria(null);
		if (null != param.getCode() && !("".equals(param.getCode()))) {
			criteria.add(Restrictions.like("code", "%" + param.getCode() + "%"));
		}
		if (null != param.getName() && !("".equals(param.getName()))) {
			criteria.add(Restrictions.like("name", "%" + param.getName() + "%"));
		}
		criteria.addOrder(Order.asc("sort"));
		
		resultPage = objService.pagedQuery(criteria, pageNo, pageSize); 
		return resultPage;
	}
}
